package org.itheima.game.org.tank.game.model

import org.itheima.game.org.tank.game.Config
import org.itheima.game.org.tank.game.business.Attackable
import org.itheima.game.org.tank.game.business.AutoMovable
import org.itheima.game.org.tank.game.business.Destroyable
import org.itheima.game.org.tank.game.business.Sufferable
import org.itheima.game.org.tank.game.enum.Direction
import org.itheima.game.org.tank.game.ext.checkCollision
import org.itheima.kotlin.game.core.Painter

/**
 * description ： 子弹
 * author : chenmengjia
 * email : cmjsp1990@163.com
 * date : 2020/3/14 17:23
 */
class Bullet(
    override var owner: View,
    override var currentDirection: Direction, create: (width: Int, height: Int) -> Pair<Int, Int>

) :
    AutoMovable, Destroyable, Attackable, Sufferable {
    private var isDestroyed = false
    private var imagePath = when (currentDirection) {
        Direction.UP -> "img/shot_top.gif"
        Direction.DOWN -> "img/shot_bottom.gif"
        Direction.LEFT -> "img/shot_left.gif"
        Direction.RIGHT -> "img/shot_right.gif"

    }
    override var blood: Int = 1


    override fun notifySuffer(attackable: Attackable): Array<View>? {
        //子弹抵消
        return arrayOf(Blast(x, y))
    }

    override var x: Int = 0
    override var y: Int = 0
    override var width = 0
    override var height = 0


    init {
        val size = Painter.size(imagePath)
        width = size[0]
        height = size[1]
        val pair = create.invoke(width, height)
        x = pair.first
        y = pair.second
    }

//给子弹一个方向


    override fun draw() {
        Painter.drawImage(imagePath, x, y)
    }

    override fun autoMove() {
        when (currentDirection) {
            Direction.UP -> y -= speed
            Direction.DOWN -> y += speed
            Direction.LEFT -> x -= speed
            Direction.RIGHT -> x += speed
        }

    }

    override var speed: Int = 8
    override fun isDestroyed(): Boolean {
        if (isDestroyed) {
            return true
        }
        if (x < -width) {
            return true
        }
        if (x > Config.gameWidth) {
            return true
        }
        if (y < -height) {
            return true
        }
        if (y > Config.gameHeight) {
            return true
        }
        return false
    }

    override var attackPower: Int = 1


    override fun isCollision(suffer: Sufferable): Boolean {
        return checkCollision(suffer)
    }

    override fun notifyAttack(suffer: Sufferable) {
        // print("bullet is received")
        //子弹销毁
        isDestroyed = true
    }

}